layui.define(function (exports) {
    var $ = layui.$;
    var Feng = {
        ctxPath: "",

        /**
         * 字典数据缓存
         */
        DictCache:{},
        addCtx: function (ctx) {
            if (this.ctxPath == "") {
                this.ctxPath = ctx;
            }
        },
        //获取点击事件的源对象
        eventParseObject: function (event) {
            event = event ? event : window.event;
            var obj = event.srcElement ? event.srcElement : event.target;
            return $(obj);
        },
        treeId: function (fields) {
            var tId = '';
            var ids = fields.split(".");
            $.each(ids, function (index, id) {
                if (index == 0) {
                    tId = id;
                } else {
                    tId = tId + Feng.UpperFirstLetter(id);
                }
            });
            return tId;
        },
        //首字母大写
        UpperFirstLetter: function (str) {
            return str.replace(/\b\w+\b/g, function (word) {
                return word.substring(0, 1).toUpperCase() + word.substring(1);
            });
        },
        //获取表单数据
        formData: function (id) {
            var formData = new Object();
            $('#' + id + ' *').each(function () {
                if (this.name != undefined && this.name != '') {
                    if ($(this).prop("tagName") == 'INPUT') {
                        formData[this.name] = $(this).val();
                    }
                    if ($(this).prop("tagName") == 'SELECT') {
                        formData[this.name] = $(this).val();
                    }
                }
            });
            return formData;
        },
        //获取字典列表
        getDictLabel: function (data, value, defaultValue) {
            data = JSON.parse(data);
            for (var i = 0; i < data.length; i++) {
                var row = data[i];
                if (row.value == value) {
                    return row.label;
                }
            }
            return defaultValue == null ? "" : defaultValue;
        },
        //判断是否为空
        params: function (id, op) {
            var params = '?op=' + (op == undefined ? 'add' : op);
            if (id != undefined) {
                params = params + "&id=" + id;
            }
            return params;
        },

        //获取文件后缀
        suffix: function (fileName) {
            var point = fileName.lastIndexOf(".");
            var type = fileName.substr(point + 1);
            return type;
        },

        height: function () {
            var h = parent.document.getElementById("mainFrame").getAttribute("height");
            return h;
        },
        /*时间戳转时间*/
        timestampToTime: function (timestamp) {
            if (timestamp == null || timestamp == '') {
                return '';
            }
            if (timestamp.toString().length != 10 && timestamp.toString().length != 13) {
                return '';
            }
            //时间戳为10位需*1000，时间戳为13位的话不需乘1000
            if (timestamp.toString().length == 10) {
                timestamp = timestamp * 1000;
            }
            var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
            Y = date.getFullYear() + '-';
            M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
            D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
            h = (date.getHours() + 1 < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
            m = (date.getMinutes() + 1 < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':';
            s = (date.getSeconds() + 1 < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
            return Y + M + D + h + m + s;
        },
        /**
         * 全局layer弹框大小
         */
        layerArea:{
            global:['80%','80%']
        },

        /**
         * 根据字典类型和value值获取label值
         * @param type
         * @param vaue
         */
        getDictLabel2:function (type,vaue) {
            if (!this.DictCache[type]){
                this.getDict(type);
            }

            var data = this.DictCache[type];
            for(var i=0,len=data.length;i<len;i++){
                var ac = data[i];
                if (ac['value'] == vaue){
                    return ac['label']
                }
            }

            return "";
        },

        /**
         * 根据type值加载数据
         * @param type
         */
        getDictList:function(type){
            if (!this.DictCache[type]){
                this.getDict(type);
            }
            return this.DictCache[type];
        },
        /**
         * 远程加载字典数据然后缓存
         * @param type
         */
        getDict:function (type) {
            $.ajax({
                url:'/sys/dict/getDictListForJs',
                type:'post',
                data:{type:type},
                async: false,
                success:function (data) {
                    if (!Feng.DictCache[type]) {
                        Feng.DictCache[type] = {};
                    }
                    Feng.DictCache[type] = data ;
                }
            })
        }
    };
    exports('feng', Feng);
});

